<?php
use app\common\library\wechat\WxPay as WxPayDriver;
abstract class WeModuleWxapp {

    public $modulename;
    public function __construct(){
        global $_W,$_GPC;
        load()->func('pdo');
        $sessionid =str_replace('we7sid-', '', $_GPC['state']);

        $core_sessions = pdo_get("core_sessions",array('sessionid'=>$sessionid,'uniacid'=>$_W['uniacid']));
        $userinfo = pdo_get('mc_members',array('openid'=>$core_sessions['openid']));
        if(!empty($core_sessions)){
            $_W['openid'] = $core_sessions['openid'];
            $_W['fans'] = $userinfo;
        }

    }
    public function pay($params){
        global $_W,$_GPC;
        load()->func('pdo');
        $config =pdo_get("wxapp",array('wxapp_id'=>$_W['uniacid']));
        if(empty($config)){
            return $this->result(1, "平台不存在");
        }
        $config = array('app_id'=>$config['app_id'],'app_secret'=>$config['app_secret'],'mchid'=>$config['mchid'],'apikey'=>$config['apikey']);
        $WxPay = new WxPayDriver($config);
        $order_no = $params['tid'];
        $openid = empty($params['user'])?$_W['openid']:$params['user'];
        $totalFee = floatval($params['fee']);
        //var_dump($totalFee);die();
        return $WxPay->unifiedorder($order_no, $openid, $totalFee);
    }
    public function __call($name, $arguments) {
        load()->func('pdo');
        $this->modulename = $_SESSION['modulename'];
        $dir = IA_ROOT . '/addons/' . $this->modulename . '/inc/wxapp';
        $function_name = strtolower(substr($name, 6));
        $file = "$dir/{$function_name}.inc.php";
        if (!file_exists($file)) {
            return error(1, "模块{$function_name}方法不存在");
        }
        if(file_exists($file)) {
            require $file;
            exit;
        }
        return null;
    }
    public function doPageCheck(){
        global $_W,$_GPC;
        $sessionid =str_replace('we7sid-', '', $_GPC['state']);
        $core_sessions = pdo_get("core_sessions",array('sessionid'=>$sessionid,'uniacid'=>$_W['uniacid']));
        $userinfo = pdo_get('mc_members',array('openid'=>$core_sessions['openid']));
        $_W['fans'] = $userinfo;
        return $this->result(0,array(), array('openid'=>$core_sessions['openid'],'userinfo'=>$userinfo));
    }
    /**
     * 获取授权信息
     */
    public function doPageOpenid(){
        global $_W,$_GPC;
        load()->func('pdo');
        $wxapp =pdo_get("wxapp",array('wxapp_id'=>$_W['uniacid']));
        if(empty($wxapp)){
            return $this->result(1, "平台不存在");
        }
        $code = $_GPC['code'];
        if(empty($code)){
            return $this->result(1, "code不存在");
        }
        $appid = $wxapp['app_id'];
        $appsecret = $wxapp['app_secret'];
        if(empty($wxapp['app_id'])||empty($wxapp['app_secret'])){
            return $this->result(1, "appid或者appsecret缺少");
        }
        $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$appsecret}&js_code={$code}&grant_type=authorization_code";
        $result = curlPost($url,array());
        $result = json_decode($result,true);
        if(!$result['errcode']){
            $_W['openid'] = $result['openid'];
            $core_sessions = pdo_get("core_sessions",array('sessionid'=>$code,'uniacid'=>$_W['uniacid']));
            if(empty($core_sessions)){
                pdo_insert("core_sessions",array('sessionid'=>$code,'openid'=>$result['openid'],'session_key'=>$result['session_key'],'uniacid'=>$_W['uniacid'],'create_time'=>time(),'update_time'=>time()));
            }
            $userinfo = pdo_get('mc_mapping_fans',array('openid'=>$result['openid'],'sessionid'=>$code));

            return $this->result(0,array(), array('openid'=>$result['openid'],'sessionid'=>$code,'unionid'=>$result['unionid'],'userinfo'=>$userinfo));
        }else{
            return $this->result(1, $result['errmsg']);
        }
    }

    public function doPageUserinfo(){
        global $_W,$_GPC;
        load()->func('pdo');
        $sessionid =str_replace('we7sid-', '', $_GPC['state']);

        $core_sessions = pdo_get("core_sessions",array('sessionid'=>$sessionid,'uniacid'=>$_W['uniacid']));
        $userinfo = json_decode(htmlspecialchars_decode($_GPC['userInfo']),true);
        if(!empty($core_sessions)){
            $mc_mapping_fans = pdo_get('mc_mapping_fans',array('openid'=>$core_sessions['openid']));
            if(empty($mc_mapping_fans)){
                $mc_members = array('openid'=>$core_sessions['openid'],'avatar'=>$userinfo['avatarUrl'],'country'=>$userinfo['country'],'city'=>$userinfo['city'],'province'=>$userinfo['province'], 'nickname'=>$userinfo['nickName'],'module'=>$_GPC['m'], 'uniacid'=>$_W['uniacid'],'createtime'=>time());
                pdo_insert("mc_members",$mc_members);
                $uid = pdo_insertid();
                pdo_insert("mc_mapping_fans",array('openid'=>$core_sessions['openid'],'uid'=>$uid,'unionid'=>$core_sessions['unionid'],'nickname'=>$userinfo['nickName'],'uniacid'=>$_W['uniacid'],'updatetime'=>time()));
            }


        }
        $userinfo = pdo_get('mc_members',array('openid'=>$core_sessions['openid']));
        $_W['fans'] = $userinfo;
        return $this->result(0,array(), array('openid'=>$core_sessions['openid'],'userinfo'=>$userinfo));
    }

    public function result($errno, $message, $data = '') {
        exit(json_encode(array(
            'errno' => $errno,
            'message' => $message,
            'data' => $data,
        )));
    }

    public function checkSign() {
        global $_GPC;
//         if (!empty($_GET) && !empty($_GPC['sign'])) {
//             foreach ($_GET as $key => $get_value) {
//                 if (!empty($get_value) && $key != 'sign') {
//                     $sign_list[$key] = $get_value;
//                 }
//             }
//             ksort($sign_list);
//             $sign = http_build_query($sign_list, '', '&') . $this->token;
//             return md5($sign) == $_GPC['sign'];
//         } else {
//             return false;
//         }
    }

}


?>